package org.example.day06;

/**
 * 第二章节:2.3.1.51: synchronized代码块也具有增加可见性的作用
 *      case1:出现死循环,线程B改变的自己的私有内存，无法同线程A的私有内存达到同步
 **/
public class MyThread5 {
    private boolean isContinue = true;
    public void runMethod(){
        while(isContinue == true){

        }
        System.out.println("停下来了");
    }
    public void stopMethod(){
        isContinue = false;
    }

    public static void main(String[] args) {
        try {
            MyThread5 m5 = new MyThread5();
            Thread5A ta = new Thread5A(m5);
            ta.start();
            Thread.sleep(1000);
            Thread5B tb = new Thread5B(m5);
            tb.start();
            System.out.println("以发动B去停止A线程");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Thread5A extends Thread{
    private MyThread5 m5;

    public Thread5A(MyThread5 m5) {
        this.m5 = m5;
    }

    @Override
    public void run() {
        m5.runMethod();
    }
}

class Thread5B extends Thread{
    private MyThread5 m5;

    public Thread5B(MyThread5 m5) {
        this.m5 = m5;
    }

    @Override
    public void run() {
        m5.stopMethod();
    }
}
